<?php
/**
 * 简单管理
 * 
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 *
 */

namespace m;
class base_ad extends ad
{
    function index_home()
    {
        $this->PU->check($this->auth_m);
        $this->title = \lang::g($this->title);
        $this->t_var['res'] = $this->home();
        
		$this->setTemplate('home');
    }
    function home()
    {
        return $this->db->get_page(array(), $this->table);
    }
    function index_post()
    {
        $this->title = \lang::g($this->title);
        $this->PU->check($this->auth_m);
    	$this->t_var['data'] = $this->getOne($_GET['id']);
        $this->setTemplate('post');
        $this->dispLayout = 'sub';
    }
    function index_save()
    {
        $this->PU->check($this->auth_m);
        $data = $_POST['data'];
        $id = $_POST['data_id'];
        if ($this->save($data, $id))
		 {
			if ($id)
				\log::write($this->table, 'edit', $id);
			else {
				$id = $this->db->insert_id();
				\log::write($this->table, 'add', $id);
			 }
			ajax_json(array('status'=>TRUE, 'id'=>$id));
		 }
        ajax_json(\lang::g('save fail')); 
    }
    function save($data, $id=NULL)
    {
        $where =  $id ? "id=". intval($id) : '';
        return $this->db->save($this->table, $data, $where);
    }
    function index_del()
    {
        $this->PU->check($this->auth_m);
		$id = intval_ids($_GET['id']);
        $this->del($id);
    }
    function del($id)
    {
		if ($this->db->del($this->table, "id IN($id)"))
		 {
			\log::write($this->table, 'del', $id);
			ajax_json(TRUE);	
		 }
		ajax_json(\lang::g( 'Over'));
    }
    
    // g
    function get()
    {
        return $this->db->get($this->table);
    }
    function getOne($id)
    {
        return $this->db->getOne($this->table, '*', 'id='. intval($id));
    }
   
    /**
     * 
     * @param string $f
     * @param array $p
     */
	function setTemplate($f, $p=array())
	{
		$this->loadTemplate($this->t_p .DIRECTORY_SEPARATOR .$f,  $p);
	}
}
?>
